# @Time : 2021/5/14 11:42
# @Author : Fioman 
# @Phone : 13149920693
from settings import *
from tools.hd_logger import HdLogger
from tools.common_tools import get_time_now_string
import cv2 as cv
import numpy as np
import json

"""
将图片保存进电脑设备中的模块,这个模块主要用来保存设备中的图片.包括标定的时候,或者是其他的时候的图片一并保存进来.
"""
logger = HdLogger()


def save_cutter_cam_cal_image(keepIndex, image, endWith="original"):
    """
    保存铡刀标定的时候的图片,铡刀标定的时候的图片保存,标定的原图,还有结果图都要保存.
    :param keepIndex:要保存的图像编号
    :param image:要保存的图片
    :param endWith:是否加后缀,比如有的图片加个res比较好
    :return:
    """
    try:
        keepDir = CUTTER_CAM_CAL_DIR
        fileName = "{}--{}--{}.bmp".format(keepIndex, get_time_now_string(), endWith)
        cv.imwrite(os.path.join(keepDir, fileName), image)
    except Exception as e:
        errorInfo = "in save_cutter_cam_cal_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def delete_exist_index_image(keepDir, boardIndex):
    """
    遍历保存图像的目录,删除将要保存的相同id的图片
    :param keepDir:图片保存目录
    :param boardIndex:图像id序列号
    :return:
    """
    for root, dirs, files in os.walk(keepDir):
        for file in files:
            try:
                index = file.split('--')[0]
                if index == str(boardIndex):
                    os.remove(os.path.join(root, file))
            except Exception as e:
                print(str(e))

def delete_exist_index_image_for_cutter(keepDir, boardIndex):
    """
    遍历保存图像的目录,删除将要保存的相同id的图片
    :param keepDir:图片保存目录
    :param boardIndex:图像id序列号
    :return:
    """
    for root, dirs, files in os.walk(keepDir):
        for file in files:
            try:
                index = file.split('--')[0]
                if int(index) % 1000 == int(boardIndex) % 1000:
                    os.remove(os.path.join(root, file))
            except Exception as e:
                print(str(e))


def save_totalboard_error_image(keepIndex, imageOriginal, imageRes,jointNumber):
    """
    保存物料检测未通过的图片
    :param keepIndex: 图片的索引
    :param imageOriginal: 原图
    :param imageRes: 识别的结果图
    :return:
    """
    try:
        if jointNumber == "2":
            dirUsed = RAW_TOTALBOARD_ERROR_DIR
        elif jointNumber == "3":
            dirUsed = RAW_TOTALBOARD_ERROR_DIR_THREE
        else:
            dirUsed = RAW_TOTALBOARD_ERROR_DIR_ONE

        delete_exist_index_image(dirUsed, keepIndex)
        fileNameOriginal = "{}--{}--orginal.bmp".format(keepIndex, get_time_now_string())
        fileNameRes = "{}--{}--res.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(dirUsed, fileNameOriginal), imageOriginal)
        cv.imwrite(os.path.join(dirUsed, fileNameRes), imageRes)
    except Exception as e:
        errorInfo = "in save_totalBoard_error_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_three_totalboard_error_image(keepIndex, imageOriginal, imageRes):
    """
    保存三拼物料检测未通过的图片
    :param keepIndex: 图片的索引
    :param imageOriginal: 原图
    :param imageRes: 识别的结果图
    :return:
    """
    try:
        delete_exist_index_image(RAW_TOTALBOARD_ERROR_DIR_THREE, keepIndex)
        fileNameOriginal = "{}--{}--orginal.bmp".format(keepIndex, get_time_now_string())
        fileNameRes = "{}--{}--res.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(RAW_TOTALBOARD_ERROR_DIR_THREE, fileNameOriginal), imageOriginal)
        cv.imwrite(os.path.join(RAW_TOTALBOARD_ERROR_DIR_THREE, fileNameRes), imageRes)
    except Exception as e:
        errorInfo = "in save_three_totalboard_error_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_calc_ok_image(keepIndex, keepImage, widthPix, heightPix, upLineOffset, downLineOffset,isFanzhuan="否"):
    """
    保存首板的图,并且保存切线的位置信息.
    :param keepIndex:保存的图像编号
    :param keepImage:要保存到额图像
    :param widthPix:图像宽度像素值
    :param heightPix:图像高度像素值
    :param upLineOffset:上框位置
    :param downLineOffset:下框位置
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_OK_DIR  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_OK_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--u_{}_{}_{}_{}--d_{}_{}_{}_{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix,
                                                                                 upLineOffset[0], upLineOffset[1], upLineOffset[2], upLineOffset[3],
                                                                                 downLineOffset[0], downLineOffset[1], downLineOffset[2],
                                                                                 downLineOffset[3])
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_three_calc_ok_image(keepIndex, keepImage, widthPix, heightPix, upLineOffset, downLineOffset,isFanzhuan="否"):
    """
    保存三拼首板的图,并且保存切线的位置信息.
    :param keepIndex:保存的图像编号
    :param keepImage:要保存到额图像
    :param widthPix:图像宽度像素值
    :param heightPix:图像高度像素值
    :param upLineOffset:上框位置
    :param downLineOffset:下框位置
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_OK_DIR_THREE  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_OK_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--u_{}_{}_{}_{}--d_{}_{}_{}_{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix,
                                                                                 upLineOffset[0], upLineOffset[1], upLineOffset[2], upLineOffset[3],
                                                                                 downLineOffset[0], downLineOffset[1], downLineOffset[2],
                                                                                 downLineOffset[3])
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_one_calc_ok_image(keepIndex, keepImage, widthPix, heightPix, upLineOffset, downLineOffset,isFanzhuan="否"):
    """
    保存三拼首板的图,并且保存切线的位置信息.
    :param keepIndex:保存的图像编号
    :param keepImage:要保存到额图像
    :param widthPix:图像宽度像素值
    :param heightPix:图像高度像素值
    :param upLineOffset:上框位置
    :param downLineOffset:下框位置
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_OK_DIR_ONE  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_OK_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--u_{}_{}_{}_{}--d_{}_{}_{}_{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix,
                                                                                 upLineOffset[0], upLineOffset[1], upLineOffset[2], upLineOffset[3],
                                                                                 downLineOffset[0], downLineOffset[1], downLineOffset[2],
                                                                                 downLineOffset[3])
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_cut_line_image(keepIndex, keepImage, widthPix, heightPix, endwith="calc",isFanzhuan="否"):
    """
    保存切线图,高和宽各自缩放两倍
    :param keepIndex: 保存的图像编号
    :param keepImage: 要保存的图像
    :param widthPix: 宽度像素值
    :param heightPix: 高度像素值
    :param endwith: 是识别出来的,还是标注的图
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_CUT_LINE_DIR  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_CUT_LINE_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        sizeScale = 2
        newWidth, newHeight = keepImage.shape[1] // sizeScale, keepImage.shape[0] // sizeScale
        keepImage = cv.resize(keepImage.copy(), (newWidth, newHeight), cv.INTER_AREA)
        fileName = "{}--{}--w_{}--h_{}--{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix, endwith)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_cut_line_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_three_cut_line_image(keepIndex, keepImage, widthPix, heightPix, endwith="calc",isFanzhuan="否"):
    """
    保存三拼切线图,高和宽各自缩放两倍
    :param keepIndex: 保存的图像编号
    :param keepImage: 要保存的图像
    :param widthPix: 宽度像素值
    :param heightPix: 高度像素值
    :param endwith: 是识别出来的,还是标注的图
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_CUT_LINE_DIR_THREE  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_CUT_LINE_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        sizeScale = 2
        newWidth, newHeight = keepImage.shape[1] // sizeScale, keepImage.shape[0] // sizeScale
        keepImage = cv.resize(keepImage.copy(), (newWidth, newHeight), cv.INTER_AREA)
        fileName = "{}--{}--w_{}--h_{}--{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix, endwith)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_cut_line_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_one_cut_line_image(keepIndex, keepImage, widthPix, heightPix, endwith="calc",isFanzhuan="否"):
    """
    保存三拼切线图,高和宽各自缩放两倍
    :param keepIndex: 保存的图像编号
    :param keepImage: 要保存的图像
    :param widthPix: 宽度像素值
    :param heightPix: 高度像素值
    :param endwith: 是识别出来的,还是标注的图
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_CUT_LINE_DIR_ONE  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_CUT_LINE_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        sizeScale = 2
        newWidth, newHeight = keepImage.shape[1] // sizeScale, keepImage.shape[0] // sizeScale
        keepImage = cv.resize(keepImage.copy(), (newWidth, newHeight), cv.INTER_AREA)
        fileName = "{}--{}--w_{}--h_{}--{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix, endwith)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_cut_line_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_calc_error_image(keepIndex, keepImage, widthPix, heightPix, upLineOffset, downLineOffset,isFanzhuan="否"):
    """
    保存失败的图,并且保存切线的位置信息.
    :param keepIndex:保存的图像编号
    :param keepImage:要保存到额图像
    :param widthPix:图像宽度像素值
    :param heightPix:图像高度像素值
    :param upLineOffset:上框位置
    :param downLineOffset:下框位置
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_FAIL_DIR  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_FAIL_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--u_{}_{}_{}_{}--d_{}_{}_{}_{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix,
                                                                                 upLineOffset[0], upLineOffset[1], upLineOffset[2], upLineOffset[3],
                                                                                 downLineOffset[0], downLineOffset[1], downLineOffset[2],
                                                                                 downLineOffset[3])
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_three_calc_error_image(keepIndex, keepImage, widthPix, heightPix, upLineOffset, downLineOffset,isFanzhuan="否"):
    """
    保存失败的图,并且保存切线的位置信息.
    :param keepIndex:保存的图像编号
    :param keepImage:要保存到额图像
    :param widthPix:图像宽度像素值
    :param heightPix:图像高度像素值
    :param upLineOffset:上框位置
    :param downLineOffset:下框位置
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_FAIL_DIR_THREE  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_FAIL_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--u_{}_{}_{}_{}--d_{}_{}_{}_{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix,
                                                                                 upLineOffset[0], upLineOffset[1], upLineOffset[2], upLineOffset[3],
                                                                                 downLineOffset[0], downLineOffset[1], downLineOffset[2],
                                                                                 downLineOffset[3])
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_one_calc_error_image(keepIndex, keepImage, widthPix, heightPix, upLineOffset, downLineOffset,isFanzhuan="否"):
    """
    保存失败的图,并且保存切线的位置信息.
    :param keepIndex:保存的图像编号
    :param keepImage:要保存到额图像
    :param widthPix:图像宽度像素值
    :param heightPix:图像高度像素值
    :param upLineOffset:上框位置
    :param downLineOffset:下框位置
    :return:
    """
    try:
        if isFanzhuan == "否":
            keepDir = RAW_FAIL_DIR_ONE  # 首板尺寸确认保存的图片
        else:
            keepDir = RAW_FAIL_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--u_{}_{}_{}_{}--d_{}_{}_{}_{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix,
                                                                                 upLineOffset[0], upLineOffset[1], upLineOffset[2], upLineOffset[3],
                                                                                 downLineOffset[0], downLineOffset[1], downLineOffset[2],
                                                                                 downLineOffset[3])
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_final_check_image(keepIndex, image1, image2):
    """
    保存终点检测的图片
    :param keepIndex:保存的图像编号
    :param image1: 1号相机的图
    :param image2: 2号相机的图
    :return:
    """
    try:
        keepDir = CUTTER_RAW_FINAL_DIR  # 首板尺寸确认保存的图片
        delete_exist_index_image_for_cutter(keepDir, keepIndex)
        fileName = "{}--{}--final_cam1.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), image1)
        fileName = "{}--{}--final_cam2.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), image2)
    except Exception as e:
        errorInfo = "in save_final_check_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_three_final_check_image(keepIndex, image1, image2):
    """
    保存终点检测的图片
    :param keepIndex:保存的图像编号
    :param image1: 1号相机的图
    :param image2: 2号相机的图
    :return:
    """
    try:
        keepDir = CUTTER_RAW_FINAL_DIR_THREE  # 首板尺寸确认保存的图片
        delete_exist_index_image_for_cutter(keepDir, keepIndex)
        fileName = "{}--{}--final_cam1.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), image1)
        fileName = "{}--{}--final_cam2.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), image2)
    except Exception as e:
        errorInfo = "in save_final_check_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_final_check_image_res(keepIndex, image1, image2):
    """
    保存终点检测的图片
    :param keepIndex:保存的图像编号
    :param image1: 1号相机的图
    :param image2: 2号相机的图
    :return:
    """
    try:
        keepDir = CUTTER_RAW_FINAL_RES_DIR  # 首板尺寸确认保存的图片
        delete_exist_index_image_for_cutter(keepDir, keepIndex)
        scale = 3
        keepImage1 = cv.resize(image1.copy(), (image1.shape[1] // scale, image1.shape[0] // scale), cv.INTER_AREA)
        keepImage2 = cv.resize(image2.copy(), (image2.shape[1] // scale, image2.shape[0] // scale), cv.INTER_AREA)
        fileName = "{}--{}--final_res_cam1.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), keepImage1)
        fileName = "{}--{}--final_res_cam2.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), keepImage2)
    except Exception as e:
        errorInfo = "in save_final_check_image_res() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_three_final_check_image_res(keepIndex, image1, image2):
    """
    保存终点检测的图片
    :param keepIndex:保存的图像编号
    :param image1: 1号相机的图
    :param image2: 2号相机的图
    :return:
    """
    try:
        keepDir = CUTTER_RAW_FINAL_RES_DIR_THREE  # 首板尺寸确认保存的图片
        delete_exist_index_image_for_cutter(keepDir, keepIndex)
        scale = 3
        keepImage1 = cv.resize(image1.copy(), (image1.shape[1] // scale, image1.shape[0] // scale), cv.INTER_AREA)
        keepImage2 = cv.resize(image2.copy(), (image2.shape[1] // scale, image2.shape[0] // scale), cv.INTER_AREA)
        fileName = "{}--{}--final_res_cam1.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), keepImage1)
        fileName = "{}--{}--final_res_cam2.bmp".format(keepIndex, get_time_now_string())
        cv.imwrite(os.path.join(keepDir, fileName), keepImage2)
    except Exception as e:
        errorInfo = "in save_final_check_image_res() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_begin_check_image(keepIndex, keepImage, endwith="first"):
    """
    保存铡刀起点检测的图片
    :param keepIndex:
    :param keepImage:
    :param endwith:
    :return:
    """
    try:
        keepDir = CUTTER_RAW_BEGIN_DIR  # 首板尺寸确认保存的图片
        delete_exist_index_image_for_cutter(keepDir, keepIndex)
        fileName = "{}--{}--{}.bmp".format(keepIndex, get_time_now_string(), endwith)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_begin_check_res_image(keepIndex, keepImage, endwith="first"):
    """
    保存铡刀起点检测的结果图片
    :param keepIndex:
    :param keepImage:
    :param endwith:
    :return:
    """
    try:
        keepDir = CUTTER_RAW_BEGIN_RES_DIR  # 首板尺寸确认保存的图片
        delete_exist_index_image_for_cutter(keepDir, keepIndex)
        fileName = "{}--{}--{}.bmp".format(keepIndex, get_time_now_string(), endwith)
        scale = 3
        keepImage = cv.resize(keepImage.copy(), (keepImage.shape[1] // scale, keepImage.shape[0] // scale), cv.INTER_AREA)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)

def save_first_board_cut_image(keepIndex, keepImage, widthPix, heightPix, endwith="calc",isFanzhuan="否"):
    try:
        if isFanzhuan == "否":
            keepDir = FIRST_CUT_BOARD_DIR  # 首板尺寸确认保存的图片
        else:
            keepDir = FIRST_CUT_BOARD_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix, endwith)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_size_is_ok_image(keepIndex, keepImage, widthPix, heightPix, endwith="calc",isFanzhuan="否"):
    """
    保存尺寸确认的图片,文件后缀后calc是计算的结果,文件后缀是label,是人工标注的尺寸
    :param keepIndex: 保存的物料号
    :param keepImage: 要保存的图像
    :param widthPix: 宽度的像素值
    :param heightPix: 高度的像素值
    :param endwith: 文件后缀
    :return:
    """
    try:
        keepIndex = keepIndex.replace('/','_').strip()
        if isFanzhuan == "否":
            keepDir = SIZE_CHECK_OK_DIR  # 首板尺寸确认保存的图片
        else:
            keepDir = SIZE_CHECK_OK_DIR_FANZHUAN
        delete_exist_index_image(keepDir, keepIndex)
        fileName = "{}--{}--w_{}--h_{}--{}.bmp".format(keepIndex, get_time_now_string(), widthPix, heightPix, endwith)
        cv.imwrite(os.path.join(keepDir, fileName), keepImage)
    except Exception as e:
        errorInfo = "in save_size_is_ok_image() error:{}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def save_train_dataset(upBox, downBox, totalBoard, fileName):
    """
    保存人工智能训练队额图片
    :param upBox: 上框位置信息
    :param downBox: 下框位置信息
    :param totalBoard: 整个物料图
    :param fileName: 文件名称
    :return: 
    """
    keepDir = TRAIN_DATASET_DIR
    keepColorDir = os.path.join(TRAIN_DATASET_DIR, "train_dataset_jd_color")  # 保存那些画线了的dir
    if not os.path.exists(keepDir):
        os.mkdir(keepDir)
    if not os.path.exists(keepColorDir) and DEVELOP:
        os.mkdir(keepColorDir)
    boardHeight = totalBoard.shape[0]
    bigHalfSize = int(boardHeight * 0.55)
    upBoard = totalBoard[:bigHalfSize]
    downBoard = totalBoard[-bigHalfSize:]

    # 高大于宽,右侧补零,size用高,否则size用宽,最后要得到的结果是一个正方形
    upSize = max(upBoard.shape)
    padUp = np.zeros((upSize, upSize), dtype='uint8')
    padUp[:upBoard.shape[0], :upBoard.shape[1]] = upBoard  # 将原来的图片补充上来,然后剩余的地方补充为黑色,得到上面的图.

    downSize = max(downBoard.shape)
    padDown = np.zeros((downSize, downSize), dtype='uint8')
    padDown[:downBoard.shape[0], :downBoard.shape[1]] = downBoard

    # 得到了上下的两张图.
    upBox = np.array(upBox).astype('int')  # 大图上的坐标,这里可以不变,因为截取完之后,上面的图的坐标并没有变化
    downBox = (np.array(downBox) - (0, (totalBoard.shape[0] - bigHalfSize))).astype('int')  # 这里要把大图上的坐标,转换为小图上,所有的y都要减去一个数.矩阵操作.坐标y值,会以此减去后面的值.

    # 获取上面矩形的一些信息
    upRect = cv.minAreaRect(upBox)
    (upCenterX, upCenterY), (upWidth, upHeight), upAngle = upRect
    upAngle = 90 + upAngle if upAngle < -70 else upAngle  # 调整 angle

    downRect = cv.minAreaRect(downBox)
    (downCenterX, downCenterY), (downWidth, downHeight), downAngle = downRect
    downAngle = 90 + downAngle if downAngle < -70 else downAngle  # 调整angle

    # 将用来训练的识别的结果保存到json文件当中去.
    upRectJson = [upCenterX / upSize, upCenterY / upSize, upWidth / upSize, upHeight / upSize, upAngle]
    downRectJson = [downCenterX / downSize, downCenterY / downSize, downWidth / downSize, downHeight / downSize, downAngle]

    upBoxJson = upBox / upSize
    downBoxJson = downBox / downSize

    # 然后是画出轮廓以及保存图片
    # 准备画布,转换为三通道彩色图片,以便于画出轮廓线
    if DEVELOP:
        upPoints = np.int0(cv.boxPoints(upRect))
        downPoints = np.int0(cv.boxPoints(downRect))
        upImageColor = cv.cvtColor(padUp, cv.COLOR_GRAY2BGR)
        downImageColor = cv.cvtColor(padDown, cv.COLOR_GRAY2BGR)
        cv.drawContours(upImageColor, [upPoints], 0, (0, 255, 0), 3)
        cv.drawContours(downImageColor, [downPoints], 0, (0, 255, 0), 3)
        # 将彩色图片保存到文件中.如果是开发者模式
        cv.imwrite(os.path.join(keepColorDir, fileName[:-4] + "_up.bmp"), cv.resize(upImageColor, (500, 500)))
        cv.imwrite(os.path.join(keepColorDir, fileName[:-4] + "_down.bmp"), cv.resize(downImageColor, (500, 500)))
    # 将图片分别保存到目标文件中
    cv.imwrite(os.path.join(keepDir, fileName[:-4] + "_up.bmp"), cv.resize(padUp, (500, 500)))
    cv.imwrite(os.path.join(keepDir, fileName[:-4] + "_down.bmp"), cv.resize(padDown, (500, 500)))

    # 将板的信息写入到json文件中
    save_single_board_res(keepDir, "singles.json", fileName[:-4], upBoxJson.tolist(), downBoxJson.tolist(), upRectJson, downRectJson)


def save_single_board_res(keepDir, jsonName, fileName, upPoints, downPoints, upRect, downRect):
    """
    保存单个板的信息,将板的信息保存起来,保存为json文件,用作后面的训练.
    :param keepDir: 保存json文件的根目录
    :param jsonName: json文件的名称
    :param fileName: 图片的名称
    :param upPoints:   在原来的整块图像上的上板四个顶点坐标
    :param downPoints: 在原来的整块图像上的下板的四个顶点坐标
    :param upRect:  在新的训练图上 上板的矩形框的位置信息,包括中心点,长宽,以及角度,除了角度都用用百分比进行表示的.
    :param downRect:在新的训练图上 下板的矩形框的位置信息,包括中心点,长宽,以及角度,除了角度都是用百分比进行表示的.
    :return:
    """
    jsonPath = os.path.join(keepDir, jsonName)
    firstKeep = False
    if not os.path.exists(jsonPath):  # 如果文件不存在就创建
        with open(jsonPath, "w") as f:
            firstKeep = True
            f.close()
    data = {}
    try:
        with open(jsonPath, "r") as f:
            if not firstKeep:  # 如果不是第一次保存
                data = json.load(f)  # 通过json加载文件
            data[fileName + "_up.bmp"] = {
                "points": upPoints,
                "rect": upRect,
            }
            data[fileName + "down.bmp"] = {
                "points": downPoints,
                "rect": downRect,
            }
        with open(jsonPath, "w") as f:  # 注意python中没有rw模式,也就是既可以读又可以写的模式.
            json.dump(data, f)  # 数据写入到文件中.
    except Exception as e:
        errorInfo = "in save_single_board_res() error: {}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)


def delete_total_cutter_cal_image():
    """
    删除所有的标定用图片
    :return:
    """
    deleteDir = CUTTER_CAM_CAL_DIR
    try:
        for root, dirs, files in os.walk(deleteDir):
            for file in files:
                os.remove(os.path.join(root, file))
    except Exception as e:
        errorInfo = "in delete_total_cutter_cal_image() error: {}".format(str(e))
        print(errorInfo)
        logger.debug(errorInfo)
        return errorInfo
    return "ok"


def open_cutter_cam_cal_dir():
    """
    打开铡刀标定时的图片目录
    :return:
    """
    ret = os.system("explorer.exe {}".format(CUTTER_CAM_CAL_DIR))
    return "ok" if ret == 1 else ret


def open_count_board_size_dir():
    """
    打开用于计算和统计物料宽高的目录
    :return:
    """
    ret = os.system("explorer.exe {}".format(COUNT_BOARD_SIZE_DIR))
    return "ok" if ret == 1 else ret


def open_line_cam_cal_image_dir():
    """
    打开线阵相机标定点额目录
    :return:
    """
    ret = os.system("explorer.exe {}".format(CALIBRATION_DIR))
    return "ok" if ret == 1 else ret


def open_debug_dir():
    """
    打开日志文件的目录
    :return:
    """
    ret = os.system("explorer.exe {}".format(DEBUG_DIR))
    return "ok" if ret == 1 else ret


def open_dir_by_file_path(openDir):
    """
    根据要打开的目录路径,打开某个目录.
    :param openDir: 要打开的目录路径
    :return:
    """
    ret = os.system("explorer.exe {}".format(openDir))
    return "ok" if ret == 1 else ret


def get_image_index_by_path(imagePath):
    """
    根据图像目录,获取所有的图像编号
    :param imagePath: 图像的目录
    :return:
    """
    if not os.path.exists(imagePath):
        return []
    fileNames = []
    for root, dirs, files in os.walk(imagePath):
        for file in files:
            if root != imagePath:
                continue
            if file[-4:] != ".bmp":
                continue
            fileNames.append(file)

    indexList = []
    for fileName in fileNames:
        if "--" in fileName:
            indexFinded = fileName.split("--")[0]
            timeFinded = fileName.split("--")[1]
            indexList.append(indexFinded + "--" + timeFinded)
    return indexList


def get_image_by_image_path_and_image_index(findPath, indexTimeStr):
    """
    根据文件路径以及图像编号和时间去获取图片.
    :param findPath: 遍历的目录
    :param indexTimeStr: 图片名称,包含图像编号和时间的字符串.
    :return:
    """
    if not os.path.exists(findPath):
        return False, "查找的目录不存在", indexTimeStr
    fileNames = []
    for root, dirs, files in os.walk(findPath):
        for file in files:
            if root != findPath:
                continue
            if file[-4:] != ".bmp":
                continue
            fileNames.append(file)

    imageFinded = [fileName for fileName in fileNames if indexTimeStr in fileName]
    if len(imageFinded) > 0:
        imageNameFinded = imageFinded[0]
        filePath = os.path.join(findPath, imageNameFinded)
        image = cv.imread(filePath)
        return True, image, imageNameFinded
    else:
        return False, "{}的图像找不到".format(indexTimeStr), indexTimeStr


def open_database_dump_dir():
    """
    打开数据库备份的目录
    :return:
    """
    openDir = DATA_BASE_DUMP_DIR
    ret = os.system("explorer.exe {}".format(openDir))
    return "ok" if ret == 1 else ret


def open_database_import_dir():
    """
    打开数据库导入的目录
    :return:
    """
    openDir = DATA_BASE_IMPORT_DIR
    ret = os.system("explorer.exe {}".format(openDir))
    return "ok" if ret == 1 else ret


if __name__ == '__main__':
    image = np.zeros((500,500),dtype=np.uint8)
    print(image.shape)
    fileIndex = "AEW1094 2/9     "
    fileIndex = fileIndex.replace('/','_').strip()
    try:
        save_size_is_ok_image(fileIndex,image,1000,2000,"calc")
        cv.imwrite("{}--123--456.bmp".format(fileIndex),image)
    except Exception as e:
        print("出现异常: {}".format(str(e)))